package com.videoadmin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.videoadmin.dao.IUserDAO;
import com.videoadmin.pojo.User;
import com.videoadmin.utils.HibernateUtil;

public class UserDAOImpl implements IUserDAO {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#saveOrUpdate(com.domain.User)
	 */
	public void saveOrUpdate(User user) {
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.saveOrUpdate(user);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#delete(com.domain.User)
	 */
	public void delete(User user) {
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.delete(user);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#findById(int)
	 */
	public User findById(int id) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			User user = (User) s.get(User.class, id);
			return user;
		} finally {
			if (s != null)
				s.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#findByVipType(int)
	 */
	public List<User> findByVipType(int id) {
		Session s = null;
		List<User> list = new ArrayList<User>();
		try {
			s = HibernateUtil.getSession();
			list = (List<User>) s.createSQLQuery(
					"select * from User where Vip_Type_Id='" + id + "'")
					.addEntity(User.class).list();
			return list;
		} finally {
			if (s != null)
				s.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#findByName(java.lang.String)
	 */
	public User findByName(String keyword) {
		Session s = null;
		List<User> list = new ArrayList<User>();
		User user = new User();
		try {
			s = HibernateUtil.getSession();
			list = (List<User>) s.createSQLQuery(
					"select * from User where User_Name='" + keyword + "'")
					.addEntity(User.class).list();
			if (list.size() == 0)
				return null;
			else {
				user = list.get(0);
				return user;
			}
		} finally {
			if (s != null)
				s.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.video.dao.IUserDao#findByAll(java.lang.String)
	 */
	public List<User> findByAll(String keyword) {
		Session s = null;
		List<User> list = new ArrayList<User>();
		try {
			s = HibernateUtil.getSession();
			list = (List<User>) s.createSQLQuery(
					"select * from User where User_Name='" + keyword
							+ "' or Email='" + keyword + "'").addEntity(
					User.class).list();
			return list;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public boolean testUser(User user) {
		User test = new User();
		test = findByName(user.getUser_Name());
		if (test == null)
			return false;// 不存在用户
		else if (test.getUser_Password().equals(user.getUser_Password()))
			if (test.getVip_Type_ID().getVip_Type_ID() != 0) {
				return false;
			} else {
				return true;// 密码正确
			}
		else
			return false;// 密码不正确
	}

}
